//
//  LXListScrollView.m
//  LXLessDemo
//
//  Created by LX on 16/2/14.
//  Copyright © 2016年 LX. All rights reserved.
//

#import "LXListScrollView.h"
#define photoTag 45787
@implementation LXListScrollView

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor clearColor];
        [self addList];
    }
    return self;
}
-(NSMutableArray *)settingTestDatfa{
    
    NSMutableArray *att = [NSMutableArray array];
    for (int i = 0; i < 19; i++) {
            NSString *string = [NSString stringWithFormat:@"%d",i];
            [att addObject:string];
    }
    return att;
}

-(void)addList{
//    CGFloat FACE_ICON_SIZE  = (CGRectGetWidth(self.bounds) - FACE_COUNT_CLU)/3;
    NSMutableArray *array = [NSMutableArray arrayWithArray:[self settingTestDatfa]];
    NSInteger  FACE_COUNT_ALL  = [array count];
    int FACE_COUNT_CLU =4;
    NSInteger FACE_COUNT_ROW = FACE_COUNT_ALL%FACE_COUNT_CLU ? FACE_COUNT_ALL/FACE_COUNT_CLU +1 : FACE_COUNT_ALL/FACE_COUNT_CLU ;
    
    NSInteger FACE_COUNT_PAGE  = ( FACE_COUNT_ROW * FACE_COUNT_CLU );
    
    self.selectImage = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"frameSelect"]];
    self.selectImage.backgroundColor = [UIColor clearColor];

    
    CGFloat FACE_ICON_SIZE  = ((CGRectGetWidth(self.bounds) - (FACE_COUNT_CLU - 1))/FACE_COUNT_CLU);

    for (int i = 0; i < FACE_COUNT_ALL; i++) {
        
        CGFloat x = ((i % FACE_COUNT_PAGE) % FACE_COUNT_CLU) * FACE_ICON_SIZE  + (i / FACE_COUNT_PAGE * 320) + i%FACE_COUNT_CLU ;
        CGFloat y = ((i % FACE_COUNT_PAGE) / FACE_COUNT_CLU) * FACE_ICON_SIZE + i/FACE_COUNT_CLU;
        /*
         分割线为1 x = i%FACE_COUNT_CLU  y＝ i%FACE_COUNT_CLU 
         */
        UIImageView *image = [[UIImageView alloc] initWithImage:[UIImage imageNamed:array[i]]];
        if (i == 0) {
            [image setImage:[UIImage imageNamed:@"phBTN"]];
        }
        image.frame = CGRectMake( x, y, FACE_ICON_SIZE, FACE_ICON_SIZE);
        image.layer.masksToBounds = YES;
        [image setUserInteractionEnabled:YES];
        image.tag = i + photoTag;
        [image addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(photoSelectClikk:)]];
        [self addSubview:image];
        if (i == 1){
            self.selectImage.frame = image.frame;
        }


    }
    [self addSubview:self.selectImage];

    self.contentSize = CGSizeMake(0, FACE_COUNT_ROW*FACE_ICON_SIZE);
    
}
-(void)photoSelectClikk:(UITapGestureRecognizer *)sender{
    NSInteger tag = sender.view.tag - photoTag;
    self.selectImage.frame = sender.view.frame;

    if (self.selectBock) {
        self.selectBock([NSString stringWithFormat:@"%ld",(long)tag]);
    }
    NSLog(@"选择的照片%ld",(long)tag);
};
@end
